<?php
require_once '../includes/functions.php';
check_login();
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>个人信息 - 落地页管理系统</title>
    <link rel="stylesheet" href="css/admin.css">
</head>
<body>
    <div class="admin-container">
        <div class="sidebar">
            <div class="sidebar-header">
                <h2>落地页管理系统</h2>
            </div>
            <ul class="sidebar-menu">
                <li><a href="index.php">仪表盘</a></li>
                <li><a href="create_subdomain.php">创建落地页</a></li>
                <li><a href="logout.php">退出登录</a></li>
            </ul>
        </div>
        
        <div class="main-content">
            <div class="header">
                <h1>个人信息</h1>
                <div class="user-info">
                    欢迎, <?php echo htmlspecialchars($_SESSION['username']); ?>
                    <div class="user-dropdown">
                        <a href="profile.php">个人信息</a>
                        <div class="divider"></div>
                        <a href="logout.php">退出登录</a>
                    </div>
                </div>
            </div>
            
            <div class="content">
                <div class="card">
                    <div class="card-header">
                        <h2 class="card-title">修改个人信息</h2>
                    </div>
                    <form id="profileForm" onsubmit="return handleSubmit(event)">
                        <div class="form-group">
                            <label for="username">用户名</label>
                            <input type="text" id="username" name="username" class="form-control" value="<?php echo htmlspecialchars($_SESSION['username']); ?>" required>
                        </div>
                        <div class="form-group">
                            <label for="currentPassword">当前密码</label>
                            <input type="password" id="currentPassword" name="currentPassword" class="form-control" required>
                        </div>
                        <div class="form-group">
                            <label for="newPassword">新密码（如不修改请留空）</label>
                            <input type="password" id="newPassword" name="newPassword" class="form-control">
                        </div>
                        <div class="form-group">
                            <label for="confirmPassword">确认新密码</label>
                            <input type="password" id="confirmPassword" name="confirmPassword" class="form-control">
                            <div class="error-message" id="errorMessage" style="display: none;"></div>
                            <div class="success-message" id="successMessage" style="display: none;"></div>
                        </div>
                        <div class="form-actions">
                            <button type="submit" class="btn btn-success">保存修改</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>

    <script>
    async function handleSubmit(event) {
        event.preventDefault();
        
        const errorMessage = document.getElementById('errorMessage');
        const successMessage = document.getElementById('successMessage');
        errorMessage.style.display = 'none';
        successMessage.style.display = 'none';
        
        const username = document.getElementById('username').value;
        const currentPassword = document.getElementById('currentPassword').value;
        const newPassword = document.getElementById('newPassword').value;
        const confirmPassword = document.getElementById('confirmPassword').value;
        
        if (newPassword && newPassword !== confirmPassword) {
            errorMessage.textContent = '两次输入的新密码不一致';
            errorMessage.style.display = 'block';
            return;
        }
        
        if (newPassword && newPassword.length < 6) {
            errorMessage.textContent = '新密码长度不能小于6个字符';
            errorMessage.style.display = 'block';
            return;
        }
        
        try {
            const response = await fetch('save_profile.php', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                },
                body: JSON.stringify({
                    username,
                    currentPassword,
                    newPassword
                })
            });
            
            const result = await response.json();
            
            if (result.success) {
                successMessage.textContent = '个人信息修改成功！';
                successMessage.style.display = 'block';
                document.getElementById('currentPassword').value = '';
                document.getElementById('newPassword').value = '';
                document.getElementById('confirmPassword').value = '';
                
                // 更新页面上显示的用户名
                const userInfoElements = document.querySelectorAll('.user-info');
                userInfoElements.forEach(el => {
                    const welcomeText = el.childNodes[0];
                    if (welcomeText.nodeType === Node.TEXT_NODE) {
                        welcomeText.textContent = `欢迎, ${username}`;
                    }
                });
            } else {
                errorMessage.textContent = result.error || '修改失败';
                errorMessage.style.display = 'block';
            }
        } catch (error) {
            errorMessage.textContent = '发生错误，请稍后重试';
            errorMessage.style.display = 'block';
        }
        
        return false;
    }
    </script>
</body>
</html>
